/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package infoplaces2;

import java.util.LinkedList;

/**
 *
 * @author andre
 */
public class teste {

    public static void main(String[] args) {
        int[] conteudo = {0, 0, 0, 1, 1, 1, 1, 0, 0};
        int sizeWindow = 4;
        double north = 10;
        int sizeBin = 1;
        double calculateDegreeMaximum = calculateDegreeMaximum(conteudo, north, sizeBin, sizeWindow);
        System.out.println("Grau Máximo com Base Norte: "+calculateDegreeMaximum);
    }
    
    
    
    private static double calculateDegreeMaximum(int[] conteudo, double north, int sizeBin, int sizeWindow) {
        //USAR ARRAYLIST para lista circular
        double somaAnterior = Double.MIN_VALUE;
        double somaAtual = Double.MIN_VALUE;
        int index = 0;
        
        for (int i = 0; i < conteudo.length; i++) {
            somaAtual = calculaSomaJanelaComPesos(createWindow(conteudo, sizeWindow, i));
            if (somaAtual > somaAnterior) {
                somaAnterior = somaAtual;
                index = i;
            }
            System.out.println("\n***********************************************\n");
        }
        
        double degreeMaximumWithoutNorth = index*sizeBin;
        System.out.println("Maior soma: "+somaAnterior);
        System.out.println("Índice que possui a maior soma de janela: "+index);
        System.out.println("Maior angulo sem Norte: "+ degreeMaximumWithoutNorth);
        
        return converteGrauComBaseNorte(north, degreeMaximumWithoutNorth);
    }
    
    private static double converteGrauComBaseNorte(double north, double degreeMaximumWithoutNorth) {
        double grauBaseNorte = (360 - north) + degreeMaximumWithoutNorth;
        return grauBaseNorte < 360 ? grauBaseNorte : (grauBaseNorte - 360);
    }

    private static double[] createWindow(int[] conteudo, int sizeWindow, int i) {
        CircularList circularList = new CircularList(conteudo);
        
        System.out.println("Vetor Conteudo (possui o numero de votos/bin");
        circularList.print(); //para debug
        double[] vetor = new double[sizeWindow];
        
        int cont = 0;
        //o i é o ponto central da janela, o j vai
        if (sizeWindow % 2 == 0) { //tamanho da janela é par
            for (int j = i - sizeWindow / 2 + 1; j <= i + sizeWindow / 2; j++) { //anda da esquerda para a direita
                vetor[cont] = circularList.get(j);
                cont++;
            }
        } else {
            for (int j = i - sizeWindow / 2; j <= i + sizeWindow / 2; j++) { //anda da esquerda para a direita
                vetor[cont] = circularList.get(j);
                cont++;
            }
        }
        System.out.println("Janela");
        print(vetor); //para debug

        return vetor;
    }

    private static double calculaSomaJanelaComPesos(double[] vetor) {
        double soma = 0;
        int sizeWindow = vetor.length;
        //new double[sizeWindow];
        if (sizeWindow == 2) {
            soma = vetor[0] * 0.6 + vetor[1] * 0.4;
        } else {
            soma = soma + (vetor[0] * 1 / (2 * sizeWindow)) + (vetor[vetor.length - 1] * 1 / (2 * sizeWindow));
            for (int i = 1; i < vetor.length - 1; i++) {
                if (i == vetor.length / 2) {
                    soma = soma + (vetor[i] * 2 / sizeWindow);
                } else {
                    soma = soma + (vetor[i] * 1 / sizeWindow);
                }
            }
        }
        System.out.println("soma = " + soma+"\n");
        return soma;
    }

    public static void print(double[] vetor) {
        for (int i = 0; i < vetor.length; i++) {
            System.out.println("vetor[" + i + "] = " + vetor[i]);
        }
        System.out.println("");
    }

}

